package dao.impl;

import dao.CommunityDao;
import pojo.entity.Community;
import util.GetResultSet;
import util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class CommunityDaoImpl implements CommunityDao {
    @Override
    public int insert(Community community) {
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }

    @Override
    public int update(Community community) {
        return 0;
    }

    @Override
    public List<Community> selectAll(Map<String,Object> map) {
        List<Community> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select * from community where 1=1 ");
        String communityName = map.get("communityName").toString();
        if(communityName != null && !communityName.isEmpty()){
            sql.append(" and communityName like ? ");
            params.add("%"+communityName+"%");
        }
        String startTime = (String) map.get("startTime");
        if(startTime != null && !startTime.isEmpty()){
            sql.append(" and createTime between ? and ? ");
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        Object pn = map.get("pageNum");
        if(pn != null && !pn.toString().isEmpty()){
            sql.append(" limit ?,? ");
            int pageNum = Integer.parseInt((String) pn);
            int pageSize = Integer.parseInt(map.get("pageSize").toString());
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        System.out.println(sql);
        System.out.println("插入数据："+ params);
        ResultSet set = JDBCDruidUtil.query(sql.toString(), params.toArray());
        if(set != null){
            try{
                while (set.next()){
                    GetResultSet<Community> get = new GetResultSet<Community>();
                    Community community = get.get(Community.class, set);
                    list.add(community);
                }
                return list;
            }catch(SQLException e){
                e.printStackTrace();
            }finally {
                JDBCDruidUtil.close();
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public Community selectById(Integer id) {
        String sql = "select * from community where communityId = ?";
        Object[] params = new Object[]{id};
        ResultSet set = JDBCDruidUtil.query(sql, params);
        if(set != null){
            try{
                while(set.next()){
                    GetResultSet<Community> res = new GetResultSet<>();
                    return res.get(Community.class, set);
                }
            }catch(SQLException e){
                e.printStackTrace();
            }finally {
                JDBCDruidUtil.close();
            }
        }
        return null;
    }

    @Override
    public int selectCount(Map<String,Object> map) {
        List<Object> params = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select count(*) from community where 1=1 ");
        String communityName = map.get("communityName").toString();
        if(communityName != null && !communityName.isEmpty()){
            sql.append(" and communityName like ? ");
            params.add("%"+communityName+"%");
        }
        String startTime = (String) map.get("startTime");
        if(startTime != null && !startTime.isEmpty()){
            sql.append(" and createTime between ? and ? ");
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        System.out.println(sql);
        ResultSet set = JDBCDruidUtil.query(sql.toString(), params.toArray());
        if(set != null){
            try{
                while (set.next()){
                    return set.getInt(1);
                }
            }catch(SQLException e){
                e.printStackTrace();
            }finally {
                JDBCDruidUtil.close();
            }
        }
        JDBCDruidUtil.close();
        return  0;
    }
}
